package face.pay.message.handler.event;


import face.pay.dao.CpnMchInfoDao;
import face.pay.entity.CpnMchInfo;
import face.pay.message.handler.AbstractHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 子商户审核通知
 */
@Component
@Slf4j
public class MerchantCheckEventHandler extends AbstractHandler {


    @Autowired
    private CpnMchInfoDao cpnMchInfoDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean doEvent(Map<String, String> map) {
        String merchantId = map.get("MerchantId");
        log.info("接受到子商户审核通知事件回调通知<<<<<<<<<<<< map:{} merchantId:{}", map.toString(), merchantId);
        CpnMchInfo record = new CpnMchInfo();
        record.setMerchant_id(Integer.parseInt(merchantId));
        try {
            CpnMchInfo cpnMchInfo = this.cpnMchInfoDao.queryCpnMchInfo(record);
            if ("1".equals(map.get("IsPass"))) {
                cpnMchInfo.setStatus(2);
                cpnMchInfo.setReason("审核通过");
                this.cpnMchInfoDao.update(cpnMchInfo);
                log.info("子商户更新成功>>>>>审核通过 merchantId:{}", merchantId);
            } else {
                cpnMchInfo.setStatus(-1);
                cpnMchInfo.setReason(map.get("Reason"));
                this.cpnMchInfoDao.update(cpnMchInfo);
                log.info("子商户更新成功>>>>>审核失败 merchantId:{} >>>>Reason{}", merchantId, map.get("Reason"));
            }
        } catch (Exception e) {
            log.error("子商户更新异常:{}", e.toString());
            return false;
        }
        return true;
    }

}
